/*/!*用户名称唯一性校验规则*!/
$.validator.addMethod("isOnlyName", function (value, element) {
    var exists = 0;
    $.ajax({
        url: "/user?m=onlyOne",
        type: 'post',
        async: false,
        data: {'memberName': value},
        dataType: 'json',
        success: function (message) {
            /!*exists = message.msg;*!/
        }
    });
    return this.optional(element) || exists;//表单不为空时才触发验证？
}, $.validator.format("用户名已注册"));*/

$.validator.addMethod("chinese",function(value,element){
        var re = /^[\u4e00-\u9fa5]+$/;
        return re.test(value);
    },
    $.validator.format("姓名必须是中文")
);

$.validator.addMethod("pwd",function(value,element){
        var re = /^(?![^a-zA-Z]+$)(?!\D+$)/;
        return re.test(value);
    },
    $.validator.format("包含数字和字母，以字母开头")
);

$.validator.addMethod("phone",function(value,element){
        var re = /^1[0-9]{10}$/;
        return re.test(value)
    },
    $.validator.format("请输入11位手机号")
);
$(function(){
    $("#regForm").validate({
            rules:{
                name:{
                    required:true,
                    rangelength:[2,10],
                    chinese:true
                },
                userName:{
                    required:true,
                    remote:{
                        url:"/user/onlyOne2",
                        type:"post",
                        dataType:"json",
                        data:{
                            userName:function(){
                                return $("input[name=userName]").val();
                            }
                        }
                    }
                },
                password:{
                    required:true,
                    pwd:true,
                    rangelength:[6,16],
                },
                conPwd:{
                    required:true,
                    equalTo:"#pwd"
                },
                imgYzm:{
                    required:true,
                    rangelength:[5,5],
                    remote:{
                        url:"/registe/tpmjy",
                        type:"post",
                        dataType:"json",
                        data:{
                            imgYzm:function(){
                                return $("input[name=imgYzm]").val();
                            }
                        }
                    }
                },
                pNumber:{
                    required:true,
                    phone:true
                },
                msgYzm:{
                    required:true,
                    rangelength:[6,6],
                    remote:{
                        url:"/registe/dxmjy",
                        type:"post",
                        dataType:"json",
                        data:{
                            msgYzm:function(){
                                return $("input[name=msgYzm]").val();
                            }
                        }
                    }
                },
                agree:{
                    required:true
                }

            },
            messages:{
                userName:{
                    required:"请输入用户名",
                    remote:"用户名已存在"
                },
                name:{
                    required:"请输入姓名",
                    rangelength:"姓名长度为2-10个字符",
                },
                password:{
                    required:"请输入密码",
                    rangelength:"密码长度为6-16个字符"

                },
                conPwd:{
                    required:"请输入确认密码",
                    equalTo:"两次密码输入不一致"
                },
                imgYzm:{
                    required:"请输入图片验证码",
                    rangelength:"必须为5位字符",
                    remote:"图片码输入错误"

                },
                pNumber:{
                    required:"请输入手机号",
                },
                msgYzm:{
                    required:"请输入短信验证码",
                    rangelength:"必须为6位字符",
                    remote:"短息码输入错误"
                },
                agree:{
                    required:"必须同意该协议协议",
                }

            },
            errorPlacement:function(error,element){
                element.closest(".row").append(error);
            },
            submitHandler:function(element,e){
                e.preventDefault();
                layer.alert(
                    '注册成功!',
                    {btn:'去登陆',icon:1},
                    function(index){
                        layer.close(index);
                        location.href = '/login'
                    }
                )
            }

        })

    $("#emailRegForm").validate({
        rules:{
            userName:{
                required:true,
                remote:{
                    url:"/user/onlyOne2",
                    type:"post",
                    dataType:"json",
                    data:{
                        userName:function(){
                            return $("#uName2").val();
                        }
                    }
                }
            },
            email:{
                required:true,
                email:true,
            },
            password:{
                required:true,
                pwd:true,
                rangelength:[6,16],
            },
            conPwd:{
                required:true,
                equalTo:"#pwd2"
            },
            imgYzm:{
                required:true,
                rangelength:[5,5],
                remote:{
                    url:"/registe/tpmjy",
                    type:"post",
                    dataType:"json",
                    data:{
                        imgYzm:function(){
                            return $("#imgYzm2").val();
                        }
                    }
                }

            },
            agree:{
                required:true
            }

        },
        messages:{
            userName:{
                required:"请输入用户名",
                remote:"用户名已存在"
            },
            email:{
                required:"请输入邮箱号",
            },
            password:{
                required:"请输入密码",
                rangelength:"密码长度为6-16个字符"

            },
            conPwd:{
                required:"请输入确认密码",
                equalTo:"两次密码输入不一致"
            },
            imgYzm:{
                required:"请输入图片验证码",
                rangelength:"必须为5位字符",
                remote:"图片码输入错误"

            },
            agree:{
                required:"必须同意该协议协议",
            }

        },
        errorPlacement:function(error,element){
            element.closest(".row").append(error);
        },
        submitHandler:function(element,e){
            e.preventDefault();
            var serialize = $(element).serialize();  //序列化表单数据
            $.ajax({
                url:"/registe/sendEmail",
                type:"post",
                dataType: "json",
                data: serialize,
                success:function(data) {
                    /*layer.msg(data.emails)*/

                    location.href = "/registe/emailReg"


                },
                error: function(){
                    parent.layer.msg("服务器忙，请稍后再试！");
                }
            });
        }
    })
})
var $pRe = $(".pRe");
var $eRe = $(".eRe");
var $phone = $("#phone");
var $email = $("#email");

// 切换注册页面
$pRe.on('click',function(){
    $(this).addClass("active");
    $eRe.removeClass("active")
    $phone.show();
    $email.hide();
})

$eRe.on('click',function(){
    $(this).addClass("active");
    $pRe.removeClass("active")
    $phone.hide();
    $email.show();
})

var $emailReg = $(".emailReg");
var $emails = $(".email");


//弹出遮罩和协议
var $xieYi = $(".xieyi");
$xieYi.on('click',function () {
    layer.open({
        type:2,
        closeBtn:1,
        title:['协议','font-size:22px;text-align:center'],
        area:['700px','800px'],
        content:'/static/regInfo.html',
        btn:'同意并继续',
        yes:function(index){
            $("input[name = agree]").prop("checked",true);
            layer.close(index);
        }
    })
})

    //刷新图片验证码
    var $chImage = $(".chImage");
    $chImage.on('click',function(){
        $(this).parent("em").prev("img").attr("src","/kaptcha.jpg?t=" + new Date().getTime());
    })

    //获取短信验证码
    var $btn_getsms = $(".btn-getsms");
    var $yzm = $(".yzm");
    var $pNumber = $(".pNumber");

    var $sms_code = $(".sms-code");
    //相关字段校验
    $sms_code.on('focus',function () {
            var flag = $("#regForm").validate().element($pNumber);
            var flag2 = $("#regForm").validate().element($yzm);
            if(!flag || !flag2) return false;
     })


    $btn_getsms.on('click',function(){

        var flag = $("#regForm").validate().element($pNumber);
        var flag2 = $("#regForm").validate().element($yzm);
        if(!flag || !flag2) return false;

        var phone = $pNumber.val();
        var code = $yzm.val();

        $btn_getsms.prop("disabled","disabled");
        $btn_getsms.css("background","black")

        $.ajax({
            url:"/registe/sendMsg",
            type:"get",
            dataType: "json",
            data: {phone:phone, code :code},
            success:function(data) {
                if(data.code == 1) {
                    var count = 60;
                    var timer = setInterval(function(){
                        if(count == 0) {
                            $btn_getsms.text("获取验证码");
                            $btn_getsms.css("background","#4ba7e0");
                            $btn_getsms.prop("disabled","");
                            clearInterval(timer);
                        }else {
                            $btn_getsms.text((count--) + "获取验证码");
                        }
                    },1000)
                    layer.msg(data.msg);
                }else{
                    $btn_getsms.prop("disabled","");
                    $btn_getsms.css("background","#4ba7e0");
                    layer.msg(data.msg);
                }

            },
            error: function(){
                $btn_getsms.prop("disabled","");
                $btn_getsms.css("background","#4ba7e0");
                layer.msg("服务器忙，请稍后再试！");

            }
        });
    })

/*/!*邮箱注册事件*!/
 $emailReg.on('click',function(e){
 registerByMail(e);
 });

 function registerByMail(e) {
 e.preventDefault()
 /!*validate 验证*!/
 var formValidate = $("#emailRegForm").validate();
 if (!formValidate.checkForm()) {
 formValidate.showErrors();
 $("#isubmit").on('click', function () {
 registerByMail();
 });
 return false;
 }
 var emails = $emails.val();
 location.href = "/user?m=emailSed&emails="+emails

 this.submit()
 }*/

/*$emailReg.on('click',function(e){
 e.preventDefault()

 var emails = $emails.val();
 location.href = "/user?m=emailSed&emails="+emails
 })*/
